<?php

namespace App\V3\V3_2_7\Models;

use App\V3\V3_2_7\Models\MaterielContentModel;
use DateTimeInterface;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class MaterielModel extends Model
{


    use HasFactory;

    protected $table = 'materiel';
    protected $dateFormat = 'U';//这个时间字段被为被格式化为UNIX 时间戳的形式存储

    //toArray 时间转换问题
    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    protected $dates = [
        'purchase_time',
        'pay_time'
    ];

    public function materielContent()
    {
        return self::hasMany(MaterielContentModel::class, 'materiel_id', 'id');
    }

    public function materielLogisticsInfo()
    {
        return self::hasMany(MaterielLogisticsInfoModel::class, 'materiel_id', 'id');
    }

    static public function getlogisticsInfo($data, $userId)
    {
        $materIdArr = [];
        $materLogisticsList = MaterielLogisticsInfoModel::getMaterLogisticsInfo($data);
        if (!empty($materLogisticsList)) {
            foreach ($materLogisticsList as $lVal) {
                if (!empty($lVal['logistics_order_no'])) {
                    $logisticsNoArr = explode(',', $lVal['logistics_order_no']);
                    if (!empty($logisticsNoArr)) {
                        foreach ($logisticsNoArr as $nVal) {
                            if (strpos($data['keyword'], $nVal) !== false) {
                                $materIdArr[]['materiel_id'] = $lVal['materiel_id'];
                            }
                        }
                    }
                }
            }
        }
        return $materIdArr;
    }

    static public function getTodayMaterielCount($condition, $startTime, $endTime)
    {
        return self::where($condition)
            ->where('delete_flag', 0)
            ->where('repeal_status', 0)
            ->whereBetween('created_at', [$startTime, $endTime])
            ->count();

    }

    static public function getMaterielId($condition, $data)
    {
        return self::select('id as materiel_id')
            ->where($condition)->where([
                'main_id' => $data['main_id'],
                'delete_flag' => 0
            ])->get()->toArray();
    }

    /**
     * 快速出入库同步价格
     * @param Request $data
     */
    static public function getSyncPrice($data)
    {
        $idArr = explode(',', $data['id']);
        $materielData = [];
        if (count($idArr) > 0) {
            foreach ($idArr as $key => $gid) {
                $materielInfo = SELF::query()->select(
                    'c.selling_price as price',
                    'b.type_id as id',
                    'b.unit_price as cost_price'
                )->from('materiel as a')
                    ->leftJoin(
                        'materiel_content as b',
                        'b.materiel_id', '=', 'a.id'
                    )->leftJoin(
                        'materiel_type as c',
                        'b.type_id', '=', 'c.id'
                    )->where([
                        'a.delete_flag' => 0,
                        'b.repeal_status' => 0,
                        'a.main_id' => $data['main_id']
                    ])->where('type_id', $gid);
                if (!empty($data['customer_id'])) {
                    $materielInfo->where('supplier_customer_id', $data['customer_id']);
                }
                $materielInfo = $materielInfo->orderBy('a.id', 'desc')->first();
                $materielData[$key] = [
                    'price' => '',
                    'id' => $gid,
                    'cost_price' => ''
                ];
                if (!empty($materielInfo)) {
                    $materielData[$key] = $materielInfo->toArray();
                }
            }
        }

        return $materielData;
    }
}
